{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/oughtinc/ergo/blob/notebooks-readme/assorted-predictions.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install --quiet poetry  # Fixes https://github.com/python-poetry/poetry/issues/532\n",
    "!pip install --quiet git+https://github.com/oughtinc/ergo.git@submit_mixture\n",
    "!pip install --quiet pendulum seaborn requests\n",
    "!pip install --quiet torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext google.colab.data_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ergo\n",
    "import pendulum\n",
    "import pandas\n",
    "import seaborn\n",
    "import requests\n",
    "import re\n",
    "import numpy as np\n",
    "from scipy import stats"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Set up Metaculus API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "metaculus = ergo.Metaculus(username=\"ought\", password=passwords[\"ought\"], api_domain=\"pandemic\")\n",
    "# metaculus = ergo.Metaculus(username=\"oughttest\", password=passwords[\"oughttest\"], api_domain=\"pandemic\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Utils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def will_x_happen_prediction (model_uncertainty, start, p_at_start, end):\n",
    "  start_to_end = (end - start).days\n",
    "  days_remaining = (end - pendulum.now()).days\n",
    "  proportion_time_remaining = days_remaining / start_to_end\n",
    "  model_prediction = p_at_start * proportion_time_remaining\n",
    "\n",
    "  return model_prediction + model_uncertainty\n",
    "\n",
    "# should be 1\n",
    "will_x_happen_prediction(0.5, pendulum.now(), 0.5, pendulum.datetime(2020,7,7))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Questions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JSON"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "question_infos = [\n",
    "    {\n",
    "        \"name\": \"What will the Seattle Police Department report as the total number of criminal offenses in March 2020?\",\n",
    "        \"id\": 3924\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"What will Washington state’s Department of Revenue report as the 2020 Q1 gross business income?\",\n",
    "        \"id\": 3923\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"Will the US federal government shut down all non-essential services by 2020-04-19?\",\n",
    "        \"id\": 3921\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"Will the Emergency Telework Act (S.3561) become law by 4/25/20?\",\n",
    "        \"id\": 3918\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"By May 1 will there be an iOS or Android app that shares an individual's COVID-19 infection status with more than 1M other users?\",\n",
    "        \"id\": 3915\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"By June 1, how many tests for COVID-19 will have been administered in the US?\",\n",
    "        \"id\": 3916\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"[short fuse] How many total confirmed deaths of novel coronavirus will be reported in the state of New York by April 2nd?\",\n",
    "        \"id\": 3934\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"What will be the US unemployment rate for March 2020?\",\n",
    "        \"id\": 3922\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"How many days will the city of New Orleans spend under lockdown between 2020-03-25 and 2020-04-15?\",\n",
    "        \"id\": 3930\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"Will Florida go under lockdown between 2020-03-25 and 2020-04-25?\",\n",
    "        \"id\": 3926\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "questions = [metaculus.get_question(question_info[\"id\"], name=question_info[\"name\"]) for question_info in question_infos]\n",
    "ergo.MetaculusQuestion.to_dataframe(questions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Models"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0. What will the Seattle Police Department report as the total number of criminal offenses in March 2020?\n",
    "\n",
    "https://pandemic.metaculus.com/questions/3924/what-will-the-seattle-police-department-report-as-the-total-number-of-criminal-offenses-in-march-2020/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def crime_model():\n",
    "  feb_total = 5090 # https://www.seattle.gov/police/information-and-data/crime-dashboard\n",
    "  mar_difference_multiplier = ergo.lognormal_from_interval(0.3, 2)\n",
    "  prediction = feb_total * mar_difference_multiplier\n",
    "  ergo.tag(prediction, \"mar_total\")\n",
    "\n",
    "crime_samples = ergo.run(crime_model, 1000)\n",
    "\n",
    "questions[0].samples = crime_samples.mar_total\n",
    "\n",
    "# questions[0].show_submission(questions[0].samples)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Apr 4\n",
    "* We're already predicting a broader range than the community on this one, and our median is close to the community median. I don't see any obvious reason to change anything\n",
    "* TODO:\n",
    "  * This one is ripe for reference-class comparison, which I haven't done at all:\n",
    "    * what does data from other states, or early data from WA, show re: impact of coronavirus on crime?\n",
    "    * what impact did other catastophes have on crime in the past?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. What will Washington state’s Department of Revenue report as the 2020 Q1 gross business income?\n",
    "\n",
    "https://pandemic.metaculus.com/questions/3923/what-will-washington-states-department-of-revenue-report-as-the-2020-q1-gross-business-income/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wa_df = pandas.read_csv(\"https://gist.githubusercontent.com/brachbach/5dc01125a44ce28e067a2dddb18f8a02/raw/12b16deecef9848c1f75432cf9aca5b61b1fd26a/WAGrossBusiness.csv\")\n",
    "wa_df[\"Total Gross\"] = wa_df[\"Total Gross\"].apply(lambda x: int(re.sub('[\\$,]', '', x)))\n",
    "wa_df[\"year\"] = wa_df[\"Year\"].apply(lambda x: int(x.split()[0]))\n",
    "quarters = wa_df[wa_df[\"Year\"].str.contains(\"Quarter\")]\n",
    "recent = quarters[quarters[\"year\"] >= 2017]\n",
    "recentish = quarters[quarters[\"year\"] >= 2008]\n",
    "worst_quarters = recentish.nsmallest(10, \"Total Gross\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "recentish"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "worst_quarters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def wa_rev_model():\n",
    "  proxy_gross = ergo.random_choice(recent[\"Total Gross\"].to_list())\n",
    "  # fuzzed to be more likely to be worse than normal rather than better\n",
    "  fuzzed = ergo.normal_from_interval(proxy_gross * 0.5, proxy_gross * 1.2)\n",
    "  ergo.tag(fuzzed, \"mar_gross\")\n",
    "\n",
    "wa_rev_samples = ergo.run(wa_rev_model, 1000)\n",
    "\n",
    "questions[1].samples = wa_rev_samples.mar_gross\n",
    "\n",
    "# questions[1].show_submission(questions[1].samples)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* IMO our prediction is grossly overconfident\n",
    "  * why do I think so:\n",
    "    * much more confident than the community\n",
    "    * my dumb model can't provide that much certainty\n",
    "    * I think that the prediction for how things would go in the absence of coronavirus should be relatively close to the 90th percentile of our estimate, which it is not\n",
    "  * TODO:\n",
    "    * I think for now, just increase the fuzz factor\n",
    "* The model predicts that revenue will turn out to have been much lower than expected for Q1 2020. It's not obvious why that'd be true\n",
    "  * stay-at-home order was not issued until 23 Mar, i.e. almost the end of the quarter\n",
    "  * TODO:\n",
    "    * switch to basically assuing that Q1 2020 will be a normal quarter, not an abnormally bad one\n",
    "    * figure out what my view on this should really be:\n",
    "      * what exactly is \"gross business income\"?\n",
    "      * how much should we expect it to have been hurt by corona before stay-at-home order?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Will the US federal government shut down all non-essential services by 2020-04-19?\n",
    "\n",
    "https://pandemic.metaculus.com/questions/3921/will-the-us-federal-government-shut-down-all-non-essential-services-by-2020-04-19/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Thinking about the question\n",
    "\n",
    "- As far as what people usually think of as \"government shutdowns\", seems like all have been caused by budgetary brinksmanship: https://www.thebalance.com/government-shutdown-3305683\n",
    "\n",
    "- Seems like no budget showdowns coming up before the question expires: http://www.crfb.org/blogs/upcoming-congressional-fiscal-policy-deadlines\n",
    "\n",
    "- So really we're just talking about coronavirus\n",
    "\n",
    "- I feel like this might resolve ambiguously: \"The president or other federal official formally announces a government shutdown\"\n",
    "\n",
    "- Also it's unclear whether no work _with pay_ would count as a \"furlough\" or not , per: \"And / or at least 200,000 federal employees are furloughed for at least 1 week\"\n",
    "\n",
    "- Not much on Google about this possibility: https://www.google.com/search?q=us+federal+government+shutdown+coronavirus&tbm=nws\n",
    "\n",
    "- Also not much about state shutdowns/furloughs\n",
    "\n",
    "- So I think this is pretty unlikely, predicting 15%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shutdown_prediction = will_x_happen_prediction(0.05, pendulum.datetime(2020,4,4), 0.08, pendulum.datetime(2020,4,19))\n",
    "\n",
    "questions[2].binary_prediction = shutdown_prediction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shutdown_prediction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Apr 4\n",
    "* Now several days later, not seeing any signs of this becoming more likely\n",
    "  * so we should lower p\n",
    "  * ~~write some simple code to do this automatically going forward~~"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Will the Emergency Telework Act (S.3561) become law by 4/25/20?\n",
    "\n",
    "https://pandemic.metaculus.com/questions/3918/will-the-emergency-telework-act-s3561-become-law-by-42520/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Thinking about the question\n",
    "\n",
    "- looking at the question and the current comment, it seems like the thing to do is to model the procedure of bills becoming laws and then use that model to predict whether this one will\n",
    "  - the first step is to build/find a qualitative model of the steps from bill to law\n",
    "  - As the comment points out, it might require something really unusual for this to become law given that Congress is currently out of session, should look into that first\n",
    "- What do I find from a quick Google News search on this? \n",
    "  - nothing from less than a week ago: https://www.google.com/search?q=Emergency+Telework+Act&source=lnms&tbm=nws\n",
    "\n",
    "##### Qualitative model of bill to law\n",
    "\n",
    "[This](https://www.zerotothree.org/resources/728-how-a-bill-becomes-a-law) seems like a good overall summary\n",
    "\n",
    "And [this](https://www.usa.gov/how-laws-are-made) seems to have similar content\n",
    "\n",
    "1.   Various things happen that we don't care about because they already happened for this bill, then: the bill gets introduced into the Senate and referred to a committee: [\"Read twice and referred to the Committee on Homeland Security and Governmental Affairs.\"](https://www.congress.gov/bill/116th-congress/senate-bill/3561/all-actions?overview=closed&KWICView=false)\n",
    "2. Committee delegates to subcommittee or submits to floor\n",
    "3. debate on floor\n",
    "4. votes in full House and Senate (by this point there may be separate bills in the Senate and House; not sure how likely that is for this bill, vs. just having one version\n",
    "5. conference committee between House and Senate\n",
    "6. full House and Senate agree to the version that came out of conference committee\n",
    "7. President signs\n",
    "\n",
    "##### Prediction\n",
    "On further reflection, just seems unlikely to me that this will become law by the specified date given that it doesn't seem to have moved forward in the last week. Assigning 15% probability for now, may go back and build the model later\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "telework_prediction = will_x_happen_prediction(0.05, pendulum.datetime(2020,4,4), 0.08, pendulum.datetime(2020,4,25))\n",
    "\n",
    "questions[3].binary_prediction = telework_prediction\n",
    "\n",
    "telework_prediction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Apr 4\n",
    "* any new news on this?\n",
    "  * no\n",
    "  * so that's a slight further negative update, I'll move us down from 0.15 to 0.13\n",
    "* TODO:\n",
    "  * reference class forecasting. What's the right reference class of bills? How likely are they to become law? How long does it take them to become law?\n",
    "    * use this to build a model of the progress of the Emergency Telework Act towards becoming law, use that model to estimate p(becomes law) by 25 Apr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###\t4. By May 1 will there be an iOS or Android app that shares an individual's COVID-19 infection status with more than 1M other users?\n",
    "\n",
    "https://pandemic.metaculus.com/questions/3915/by-may-1-will-there-be-an-ios-or-android-app-that-shares-an-individuals-covid-19-infection-status-with-more-than-1m-other-users/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Thinking about the question\n",
    "* Seems really hard to think about\n",
    "* Some related stuff:\n",
    "  * Apple has a coronavirus screening app: https://www.apple.com/covid19/ (does not seem to share your status)\n",
    "  * A friend of mine who values digital privacy but also other things posted on FB: \"Question: Under current employment law, what are the rules about whether you can require seeing the results of a medical test before hiring someone?\". This suggests to me that people would likely be open to an app like this, despite the sort of panoptic weirdness of it\n",
    "  * China:\n",
    "    * https://www.nytimes.com/2020/03/01/business/china-coronavirus-surveillance.html\n",
    "      * app assigns you a coronavirus risk assessment that's sent to the government\n",
    "      * doesn't report this to other users, so doesn't count\n",
    "  * Should look into what dating apps are doing/considering\n",
    "    * [didn't find too much](https://www.google.com/search?q=dating+app+share+coronavirus+status&source=lnms&sa=X&ved=0ahUKEwjZyIv9-MXoAhWYJzQIHYx-DGEQ_AUIDSgA&biw=1200&bih=1809&dpr=1)\n",
    "  * Seems likely that the app would star=t somewhere other than the US. East Asia seems particularly likely\n",
    "* One way to make a breakthrough on this question would be to learn about an app/feature that's in development\n",
    "* thinking about reference class, or at least just similar things -- what sort of info about themselves do users share with each other in this way?\n",
    "  * current prediction: 55%. Would have been at like 70% but surprised that we haven't already seen this come out of East Aia\n",
    "  * Can you report HIV or other STI status:\n",
    "    * on Grindr:\n",
    "      * [Yes](https://www.npr.org/sections/thetwo-way/2018/04/03/599069424/grindr-admits-it-shared-hiv-status-of-users)\n",
    "      * not sure about other STIs\n",
    "    * on Fetlife?\n",
    "      * seems like no, but that you also can't enter much other info for your profile\n",
    "    * on Tinder? \n",
    "      * No\n",
    "  * on OKCupid, you can answer a question about how you're feeling about coronavirus\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "app_prediction = will_x_happen_prediction(0.05, pendulum.datetime(2020,4,5), 0.55, pendulum.datetime(2020,5,1))\n",
    "\n",
    "questions[4].binary_prediction = app_prediction\n",
    "\n",
    "app_prediction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4 Apr update notes\n",
    "* checked the comments, nothing new + interesting\n",
    "* added in the consideration that you can answer on OKC re: how you're feeling about Coronavirus, which increased my p to 0.6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. By June 1, how many tests for COVID-19 will have been administered in the US?\t\n",
    "\n",
    "https://pandemic.metaculus.com/questions/3916/by-june-1-how-many-tests-for-covid-19-will-have-been-administered-in-the-us/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Metaculus says that this is the best data source, seems reasonable: https://covidtracking.com/api/us/daily"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "daily_corona = pandas.read_csv(\"https://covidtracking.com/api/us/daily.csv\")\n",
    "daily_corona[\"date\"] = daily_corona[\"date\"].apply(lambda x: pendulum.parse(str(x)))\n",
    "daily_corona = daily_corona.sort_values(\"date\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "daily_corona"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "seaborn.pointplot(daily_corona[\"date\"], daily_corona[\"totalTestResults\"], color=\"red\")\n",
    "seaborn.pointplot(daily_corona[\"date\"], daily_corona[\"totalTestResultsIncrease\"], color=\"green\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Original notes: Looks like there have been a constant number of new tests per day lately, so just assume that will continue to be the case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def tests_model():\n",
    "    latest_date = pendulum.instance(daily_corona.iloc[-1][\"date\"])\n",
    "    total_tests = daily_corona.iloc[-1][\"totalTestResults\"]\n",
    "    last_five_days = daily_corona.nlargest(5,\"date\")\n",
    "    avg_recent_daily_tests = last_five_days[\"totalTestResultsIncrease\"].mean()\n",
    "\n",
    "    tests_per_day_high = ergo.normal_from_interval(avg_recent_daily_tests * 1, avg_recent_daily_tests * 10)\n",
    "    tests_per_day_likely = ergo.lognormal_from_interval(avg_recent_daily_tests * 0.2, avg_recent_daily_tests * 2)\n",
    "    tests_per_day = ergo.random_choice([tests_per_day_high, tests_per_day_likely, tests_per_day_likely, tests_per_day_likely])\n",
    "    end_date = pendulum.date(2020,6,1)\n",
    "    for i in range((end_date - latest_date).days + 1):\n",
    "      date = latest_date.add(days=i)\n",
    "      total_tests = total_tests + tests_per_day\n",
    "      ergo.tag(total_tests, date.format('YYYY/MM/DD'))\n",
    "\n",
    "test_samples = ergo.run(tests_model, 1000)\n",
    "\n",
    "questions[5].samples = test_samples[\"2020/06/01\"]\n",
    "\n",
    "# questions[5].show_submission(questions[5].samples)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "questions[5].submit_from_samples(test_samples[\"2020/06/01\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Apr 4 updates\n",
    "* ~~increase number of samples to run to 1000, 100 might have been too few~~\n",
    "* My prediction distribution is way too narrow:\n",
    "  * I think so because:\n",
    "    * A priori -- there's no way I'm that confident\n",
    "    * compared to the community prediction\n",
    "  * What I'm going to do:\n",
    "    * ~~instead of sampling tests_today every day, sample tests_per_day for the whole duration. I had already identified this as a way to make the model make more sense, and it will also lead to a wider distribution~~\n",
    "    * TODO: do this in some more sophisticated way where I can more clearly express my guesses for how many tests will be administered over the whole interval, compared to now. Seems good to do, planning to come back to this\n",
    "* tests per day looks slightly less constant now\n",
    "  * TODO: consider modeling test_per_day based on something other than average tests over the past few days -- fit a quadratic or do a linear regression or something. Will skip this for now but maybe come back to it later\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. [short fuse] How many total confirmed deaths of novel coronavirus will be reported in the state of New York by April 2nd?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from this metaculus comment: https://pandemic.metaculus.com/questions/3934/short-fuse-how-many-total-confirmed-deaths-of-novel-coronavirus-will-be-reported-in-the-state-of-new-york-by-april-2nd/#comment-25503\n",
    "supposed_covid_timeseries = pandas.read_csv(\"https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv\")\n",
    "ny = supposed_covid_timeseries[supposed_covid_timeseries[\"state\"].str.contains(\"New York\")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ny"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# manually fit an exponential curve\n",
    "seaborn.pointplot(ny[\"date\"], ny[\"deaths\"], color=\"red\")\n",
    "seaborn.pointplot(ny[\"date\"], [1.28**x for x in range(0,len(ny[\"date\"]))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def death_model():\n",
    "  fit_base = 1.28\n",
    "  # mar 29 is day 29, so apr 2 is day 33\n",
    "  day_number = 33\n",
    "  fuzzed_base = ergo.lognormal_from_interval(fit_base - 0.01, fit_base + 0.01)\n",
    "  ergo.tag(fuzzed_base**day_number, \"ny_deaths\")\n",
    "\n",
    "death_samples = ergo.run(death_model, 1000)\n",
    "\n",
    "questions[6].samples = death_samples.ny_deaths\n",
    "# questions[6].show_submission(questions[6].samples)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7. What will be the US unemployment rate for March 2020?\n",
    "https://pandemic.metaculus.com/questions/3922/what-will-be-the-us-unemployment-rate-for-march-2020/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "monthly_unemployment_wide = pandas.read_csv(\"https://gist.githubusercontent.com/brachbach/d966ef4221215bdd58a5067802ded0be/raw/4a944a28eb9f222023396020913aabd507538060/monthly_unemployment_wide.csv\")\n",
    "# monthly_unemployment_wide"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "months = [\"Jan\",\"Feb\",\"Mar\",\"Apr\",\"May\",\"Jun\",\"Jul\",\"Aug\",\"Sep\",\"Oct\",\"Nov\",\"Dec\"]\n",
    "unemployment = pandas.melt(monthly_unemployment_wide, id_vars=[\"Year\"],\n",
    "                           value_vars=[\"Jan\",\"Feb\",\"Mar\",\"Apr\",\"May\",\"Jun\",\"Jul\",\"Aug\",\"Sep\",\"Oct\",\"Nov\",\"Dec\"],\n",
    "                           var_name=\"month\",\n",
    "                           value_name=\"percent_unemployment\")\n",
    "\n",
    "unemployment[\"month\"] = unemployment[\"month\"].apply(lambda name: months.index(name) + 1)\n",
    "unemployment = unemployment.sort_values(by=[\"Year\", \"month\"])\n",
    "unemployment[\"monthly_diff\"] = unemployment[\"percent_unemployment\"].diff()\n",
    "# unemployment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "biggest_monthly_increase = unemployment.nlargest(5, \"monthly_diff\")\n",
    "biggest_monthly_increase"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def unemployment_model():\n",
    "  proxy_delta = ergo.random_choice(biggest_monthly_increase[\"monthly_diff\"].to_list())\n",
    "  fuzzed_delta = ergo.normal_from_interval(proxy_delta * 0.5, proxy_delta * 2)\n",
    "  feb_rate = 3.5\n",
    "  ergo.tag(feb_rate + fuzzed_delta, \"mar_unemployment\")\n",
    "\n",
    "unemployment_samples = ergo.run(unemployment_model, 1000)\n",
    "\n",
    "questions[7].samples = unemployment_samples.mar_unemployment\n",
    "# questions[7].show_submission(questions[7].samples)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. How many days will the city of New Orleans spend under lockdown between 2020-03-25 and 2020-04-15?\n",
    "\n",
    "https://pandemic.metaculus.com/questions/3930/how-many-days-will-the-city-of-new-orleans-spend-under-lockdown-between-2020-03-25-and-2020-04-15/\n",
    "\n",
    "Predicting in the [lockdown model](https://colab.research.google.com/drive/1BRplIkEvySIWLDfL2m-I2-69ES725Hnv)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9. Will Florida go under lockdown between 2020-03-25 and 2020-04-25?\n",
    "\n",
    "https://pandemic.metaculus.com/questions/3926/will-florida-go-under-lockdown-between-2020-03-25-and-2020-04-25/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Thinking through the question\n",
    "* Which other states are currently not under lockdown?\n",
    "  * https://www.usatoday.com/story/news/nation/2020/03/21/coronavirus-lockdown-orders-shelter-place-stay-home-state-list/2891193001/\n",
    "    * lists 34 states (`$('h2').length`)\n",
    "  * https://www.wsj.com/articles/a-state-by-state-guide-to-coronavirus-lockdowns-11584749351\n",
    "    * lists 31 states (`$(\"h6\").length`), but actually Florida is on there so not all are true lockdowns\n",
    "* Hmm, resolution may well be ambiguous. Florida is already doing some partial lockdown:\n",
    "  * https://www.wsj.com/articles/florida-unlike-other-hard-hit-states-avoids-broad-coronavirus-lockdown-11585560601\n",
    "  *[https://www.wsj.com/articles/a-state-by-state-guide-to-coronavirus-lockdowns-11584749351](reports that some parts of Florida have closed down business, but not all)\n",
    "  * what if e.g. the Governor orders the businesses that ordinarily serve most Floridians to shut down, but doesn't close rural ones, and doesn't order people to stay home?\n",
    "\n",
    "Doesn't seem crazy to me to think that the Governor will maintain the current not-quite a lockdown policy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "questions[9].binary_prediction = 0.55"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Submit predictions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Convert samples to Metaculus distributions and visualize:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for question in questions:\n",
    "  if question.type == \"binary\":\n",
    "    print(f\"Prediction for {question}: {question.binary_prediction}\")\n",
    "  elif question.type == \"continuous\":\n",
    "    try:\n",
    "      question.show_submission(question.samples)\n",
    "    except:\n",
    "      print(f\"No submission or submission can't be shown for {question}\")\n",
    "    print(\"\\n\\n\")\n",
    "  else:\n",
    "    raise ValueError(\"Unknown question type!\")\n",
    "  print(\"\\n\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If everything looks good, submit the predictions!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def submit_all():\n",
    "  for question in questions:\n",
    "    if question.type == \"binary\":\n",
    "      try:\n",
    "        r = question.submit(question.binary_prediction)\n",
    "        print(f\"Submitted for {question.name}\")\n",
    "        print(f\"https://pandemic.metaculus.com{question.page_url}\")\n",
    "      except requests.exceptions.HTTPError as e:\n",
    "        print(f\"Couldn't make prediction for {question.name} -- maybe this question is now closed? See error below.\")\n",
    "        print(e)\n",
    "    elif question.type == \"continuous\":\n",
    "      try:\n",
    "        question.samples\n",
    "      except:\n",
    "        print(f\"No predictions for {question}\")\n",
    "        continue\n",
    "\n",
    "      try:\n",
    "        r = question.submit_from_samples(question.samples)\n",
    "        print(f\"Submitted for {question.name}\")\n",
    "        print(f\"https://pandemic.metaculus.com{question.page_url}\")\n",
    "      except requests.exceptions.HTTPError as e:\n",
    "        print(f\"Couldn't make prediction for {question.name} -- maybe this question is now closed? See error below.\")\n",
    "        print(e)\n",
    "    else:\n",
    "      raise ValueError(\"Unknown question type!\")\n",
    "      \n",
    "\n",
    "submit_all()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TODO\n",
    "1. consider automatically pulling all open questions and matching them to models"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
